<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="危险源编码" prop="hazardCode">
        <el-input
          v-model="queryParams.hazardCode"
          placeholder="请输入危险源编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业编码" prop="enterpriseCode">
        <el-input
          v-model="queryParams.enterpriseCode"
          placeholder="请输入企业编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="危险源名称" prop="hazardName">
        <el-input
          v-model="queryParams.hazardName"
          placeholder="请输入危险源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="危险源简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入危险源简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="危险源等级" prop="hazardLevel">
        <el-input
          v-model="queryParams.hazardLevel"
          placeholder="请输入危险源等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="R值" prop="rValue">
        <el-input
          v-model="queryParams.rValue"
          placeholder="请输入R值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重大危险源分类" prop="hazardCategory">
        <el-input
          v-model="queryParams.hazardCategory"
          placeholder="请输入重大危险源分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行政区域编码" prop="adminRegionCode">
        <el-input
          v-model="queryParams.adminRegionCode"
          placeholder="请输入行政区域编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="lon">
        <el-input
          v-model="queryParams.lon"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维度" prop="lat">
        <el-input
          v-model="queryParams.lat"
          placeholder="请输入维度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投用日期" prop="usedTime">
        <el-date-picker clearable
          v-model="queryParams.usedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择投用日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="防护范围" prop="protectionRange">
        <el-input
          v-model="queryParams.protectionRange"
          placeholder="请输入防护范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="防护人数" prop="protectionPersonNum">
        <el-input
          v-model="queryParams.protectionPersonNum"
          placeholder="请输入防护人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要负责人" prop="mainResponsiblePerson">
        <el-input
          v-model="queryParams.mainResponsiblePerson"
          placeholder="请输入主要负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要负责人联系方式" prop="mainResponsibleTel">
        <el-input
          v-model="queryParams.mainResponsibleTel"
          placeholder="请输入主要负责人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要负责人职务" prop="mainResponsiblePosition">
        <el-input
          v-model="queryParams.mainResponsiblePosition"
          placeholder="请输入主要负责人职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术负责人" prop="technicalResponsiblePerson">
        <el-input
          v-model="queryParams.technicalResponsiblePerson"
          placeholder="请输入技术负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术负责人联系方式" prop="technicalResponsibleTel">
        <el-input
          v-model="queryParams.technicalResponsibleTel"
          placeholder="请输入技术负责人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术负责人职务" prop="technicalResponsiblePosition">
        <el-input
          v-model="queryParams.technicalResponsiblePosition"
          placeholder="请输入技术负责人职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作负责人" prop="operationResponsiblePerson">
        <el-input
          v-model="queryParams.operationResponsiblePerson"
          placeholder="请输入操作负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作负责人联系方式" prop="operationResponsibleTel">
        <el-input
          v-model="queryParams.operationResponsibleTel"
          placeholder="请输入操作负责人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作负责人职务" prop="operationResponsiblePosition">
        <el-input
          v-model="queryParams.operationResponsiblePosition"
          placeholder="请输入操作负责人职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:hazard:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:hazard:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:hazard:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:hazard:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hazardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="危险源编码" align="center" prop="hazardCode" />
      <el-table-column label="企业编码" align="center" prop="enterpriseCode" />
      <el-table-column label="危险源名称" align="center" prop="hazardName" />
      <el-table-column label="危险源简称" align="center" prop="shortName" />
      <el-table-column label="危险源等级" align="center" prop="hazardLevel" />
      <el-table-column label="R值" align="center" prop="rValue" />
      <el-table-column label="重大危险源分类" align="center" prop="hazardCategory" />
      <el-table-column label="行政区域编码" align="center" prop="adminRegionCode" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="经度" align="center" prop="lon" />
      <el-table-column label="维度" align="center" prop="lat" />
      <el-table-column label="投用日期" align="center" prop="usedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="防护范围" align="center" prop="protectionRange" />
      <el-table-column label="防护人数" align="center" prop="protectionPersonNum" />
      <el-table-column label="主要负责人" align="center" prop="mainResponsiblePerson" />
      <el-table-column label="主要负责人联系方式" align="center" prop="mainResponsibleTel" />
      <el-table-column label="主要负责人职务" align="center" prop="mainResponsiblePosition" />
      <el-table-column label="技术负责人" align="center" prop="technicalResponsiblePerson" />
      <el-table-column label="技术负责人联系方式" align="center" prop="technicalResponsibleTel" />
      <el-table-column label="技术负责人职务" align="center" prop="technicalResponsiblePosition" />
      <el-table-column label="操作负责人" align="center" prop="operationResponsiblePerson" />
      <el-table-column label="操作负责人联系方式" align="center" prop="operationResponsibleTel" />
      <el-table-column label="操作负责人职务" align="center" prop="operationResponsiblePosition" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:hazard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:hazard:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改重大危险源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="危险源编码" prop="hazardCode">
          <el-input v-model="form.hazardCode" placeholder="请输入危险源编码" />
        </el-form-item>
        <el-form-item label="企业编码" prop="enterpriseCode">
          <el-input v-model="form.enterpriseCode" placeholder="请输入企业编码" />
        </el-form-item>
        <el-form-item label="危险源名称" prop="hazardName">
          <el-input v-model="form.hazardName" placeholder="请输入危险源名称" />
        </el-form-item>
        <el-form-item label="危险源简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入危险源简称" />
        </el-form-item>
        <el-form-item label="危险源等级" prop="hazardLevel">
          <el-input v-model="form.hazardLevel" placeholder="请输入危险源等级" />
        </el-form-item>
        <el-form-item label="R值" prop="rValue">
          <el-input v-model="form.rValue" placeholder="请输入R值" />
        </el-form-item>
        <el-form-item label="重大危险源分类" prop="hazardCategory">
          <el-input v-model="form.hazardCategory" placeholder="请输入重大危险源分类" />
        </el-form-item>
        <el-form-item label="行政区域编码" prop="adminRegionCode">
          <el-input v-model="form.adminRegionCode" placeholder="请输入行政区域编码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="投用日期" prop="usedTime">
          <el-date-picker clearable
            v-model="form.usedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择投用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="防护范围" prop="protectionRange">
          <el-input v-model="form.protectionRange" placeholder="请输入防护范围" />
        </el-form-item>
        <el-form-item label="防护人数" prop="protectionPersonNum">
          <el-input v-model="form.protectionPersonNum" placeholder="请输入防护人数" />
        </el-form-item>
        <el-form-item label="主要负责人" prop="mainResponsiblePerson">
          <el-input v-model="form.mainResponsiblePerson" placeholder="请输入主要负责人" />
        </el-form-item>
        <el-form-item label="主要负责人联系方式" prop="mainResponsibleTel">
          <el-input v-model="form.mainResponsibleTel" placeholder="请输入主要负责人联系方式" />
        </el-form-item>
        <el-form-item label="主要负责人职务" prop="mainResponsiblePosition">
          <el-input v-model="form.mainResponsiblePosition" placeholder="请输入主要负责人职务" />
        </el-form-item>
        <el-form-item label="技术负责人" prop="technicalResponsiblePerson">
          <el-input v-model="form.technicalResponsiblePerson" placeholder="请输入技术负责人" />
        </el-form-item>
        <el-form-item label="技术负责人联系方式" prop="technicalResponsibleTel">
          <el-input v-model="form.technicalResponsibleTel" placeholder="请输入技术负责人联系方式" />
        </el-form-item>
        <el-form-item label="技术负责人职务" prop="technicalResponsiblePosition">
          <el-input v-model="form.technicalResponsiblePosition" placeholder="请输入技术负责人职务" />
        </el-form-item>
        <el-form-item label="操作负责人" prop="operationResponsiblePerson">
          <el-input v-model="form.operationResponsiblePerson" placeholder="请输入操作负责人" />
        </el-form-item>
        <el-form-item label="操作负责人联系方式" prop="operationResponsibleTel">
          <el-input v-model="form.operationResponsibleTel" placeholder="请输入操作负责人联系方式" />
        </el-form-item>
        <el-form-item label="操作负责人职务" prop="operationResponsiblePosition">
          <el-input v-model="form.operationResponsiblePosition" placeholder="请输入操作负责人职务" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHazard, getHazard, delHazard, addHazard, updateHazard } from "@/api/basic/hazard"

export default {
  name: "Hazard",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 重大危险源表格数据
      hazardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hazardCode: null,
        enterpriseCode: null,
        hazardName: null,
        shortName: null,
        hazardLevel: null,
        rValue: null,
        hazardCategory: null,
        adminRegionCode: null,
        address: null,
        lon: null,
        lat: null,
        usedTime: null,
        protectionRange: null,
        protectionPersonNum: null,
        mainResponsiblePerson: null,
        mainResponsibleTel: null,
        mainResponsiblePosition: null,
        technicalResponsiblePerson: null,
        technicalResponsibleTel: null,
        technicalResponsiblePosition: null,
        operationResponsiblePerson: null,
        operationResponsibleTel: null,
        operationResponsiblePosition: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询重大危险源列表 */
    getList() {
      this.loading = true
      listHazard(this.queryParams).then(response => {
        this.hazardList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hazardCode: null,
        enterpriseCode: null,
        hazardName: null,
        shortName: null,
        hazardLevel: null,
        rValue: null,
        hazardCategory: null,
        adminRegionCode: null,
        address: null,
        lon: null,
        lat: null,
        usedTime: null,
        protectionRange: null,
        protectionPersonNum: null,
        mainResponsiblePerson: null,
        mainResponsibleTel: null,
        mainResponsiblePosition: null,
        technicalResponsiblePerson: null,
        technicalResponsibleTel: null,
        technicalResponsiblePosition: null,
        operationResponsiblePerson: null,
        operationResponsibleTel: null,
        operationResponsiblePosition: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加重大危险源"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getHazard(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改重大危险源"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHazard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addHazard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除重大危险源编号为"' + ids + '"的数据项？').then(function() {
        return delHazard(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/hazard/export', {
        ...this.queryParams
      }, `hazard_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
